/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.persistance;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
import java.util.TreeMap;
import nominalitzacions.domain.UrlDomainController;

/**
 *
 * @author genis.sole
 */
public class LanguageDataManager {
    private UrlDomainController UDC;
    private static String sep = System.getProperty("file.separator");


    public LanguageDataManager() {
        UDC = new UrlDomainController();
    }

    public void saveLanguage(String infPath, String irrPath, String nounsPath,
            String corpPath) throws IOException {
        String destPath = UDC.getLanguages();
        File path = new File(destPath);
        if(!path.exists()) {
            path.mkdir();
        }
        copyFile(infPath, destPath + sep + "infinitives");
        copyFile(irrPath, destPath + sep + "irregulars");
        copyFile(nounsPath, destPath + sep + "nouns");
        copyFile(corpPath, destPath + sep + "corpus");
    }

    private void copyFile(String s, String d) throws FileNotFoundException, IOException {
        File source = new File(s);
        File dest = new File(d);
        InputStream in = new FileInputStream(source);
        OutputStream out = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }


    public void saveVerb(String verb) throws IOException {
        String path = UDC.getLanguages() + "infinitives";

        FileWriter fstream = new FileWriter(path, true);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(verb);
        out.newLine();
        out.close();
    }

    public void deleteVerb(String verb) throws IOException {
        File s = new File(UDC.getLanguages() + "infinitives");
        File d = new File(UDC.getLanguages() + "infinitivesTEMP");
        d.createNewFile();
        FileWriter fstream = new FileWriter(d, true);
        BufferedWriter out = new BufferedWriter(fstream);
        Scanner sc = new Scanner(s);
        while(sc.hasNextLine()) {
            String v = sc.nextLine();
            if (!v.equals(verb)) {
                out.write(v);
                out.newLine();
            }
        }
        sc.close();
        out.close();
        s.delete();
        d.renameTo(s);
    }

    public void savePair(String alpha, String beta) throws IOException {
        String p = alpha + " " + beta;

        String path = UDC.getLanguages() + "corpus";
        
        FileWriter fstream = new FileWriter(path, true);
        BufferedWriter out = new BufferedWriter(fstream);

        out.write(p);
        out.newLine();
        
        out.close();

    }

    public void deletePair(String alpha, String beta) throws IOException {
        String pair = alpha + " " + beta;

        File s = new File(UDC.getLanguages() + "corpus");
        File d = new File(UDC.getLanguages() + "corpusTEMP");
        d.createNewFile();
        FileWriter fstream = new FileWriter(d, true);
        BufferedWriter out = new BufferedWriter(fstream);
        Scanner sc = new Scanner(s);
        while(sc.hasNextLine()) {
            String v = sc.nextLine();
            if (!v.equals(pair)) {
                out.write(v);
                out.newLine();
            }
        }
        sc.close();
        out.close();
        s.delete();
        d.renameTo(s);
    }

    public void deleteLanguage(String langName) {
        File f = new File(langName);
        dirDelete(f);
    }

    private boolean dirDelete(File dir) {
        if (dir.exists()) {
            File [] files = dir.listFiles();
            for (int i = 0; i < files.length; ++i) {
                if (files[i].isDirectory()) {
                    dirDelete(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return(dir.delete());
    }
}
